package com.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demo.entity.User;
import com.demo.service.UserService;

@Controller
@RequestMapping("/login")
public class LoginController {
	@Autowired
	private UserService uservice;

	/**
	 * 去访问登陆页面
	 * 
	 * @return
	 */
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "login";
	}
	/**
	 * 去访问注册页面
	 * @return
	 */
	@RequestMapping("/toRegister")
	public String toRegister() {
		return "register";
	}
	/**
	 * 前往登陆成功页面
	 * @return
	 */
	@RequestMapping("/toLoginSuccesss")
	public String toLoginSuccesss() {
		return "list";
	}
	/**
	 * 前往无权访问界面
	 * @return
	 */
	@RequestMapping("/goNoPermission")
	public String goNoPermission() {
		return "nopermission";
	}
	/**
	 * 登陆
	 * 
	 * @param user
	 */
	@RequestMapping("/selectByUserName")
	public String login(User user,HttpServletRequest request,HttpServletResponse response) {
		// 得到Subject
		Subject currentUser = SecurityUtils.getSubject();
		System.out.println("验证用户是否已登陆"+currentUser.isAuthenticated());
		// 验证用户是否验证，即否已经登录过，即是否设置过setRememberMe（我这么认为）
		if (!currentUser.isAuthenticated()) {
			String msg = "";
			// 把用户名和密码封装为 UsernamePasswordToken 对象
			UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
			// remembermMe记住密码,setRememberMe（）与isAuthenticated（）值相反
			//设置setRememberMe之后在一定时间内不会再次执行doGetAuthenticationInfo方法
			token.setRememberMe(true);
			System.out.println("是否设置记住我"+token.isRememberMe());
			try {
				// 执行登录.
				currentUser.login(token);

				// 登录成功...
				return "list";
			} catch (IncorrectCredentialsException e) {
				msg = "登录密码错误";
				System.out.println("登录密码错误!!!" + e);
			} catch (ExcessiveAttemptsException e) {
				msg = "登录失败次数过多";
				System.out.println("登录失败次数过多!!!" + e);
			} catch (LockedAccountException e) {
				msg = "帐号已被锁定";
				System.out.println("帐号已被锁定!!!" + e);
			} catch (DisabledAccountException e) {
				msg = "帐号已被禁用";
				System.out.println("帐号已被禁用!!!" + e);
			} catch (ExpiredCredentialsException e) {
				msg = "帐号已过期";
				System.out.println("帐号已过期!!!" + e);
			} catch (UnknownAccountException e) {
				msg = "帐号不存在";
				System.out.println("帐号不存在!!!" + e);
			} catch (UnauthorizedException e) {
				msg = "您没有得到相应的授权！";
				System.out.println("您没有得到相应的授权！" + e);
			} catch (Exception e) {
				System.out.println("出错！！！" + e);
			}
			request.setAttribute("msg", msg);
			return "loginerror";
		}
		return "list";
	}

	/**
	 * 注册
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("/register")
	public String register(User user) {
		int result = uservice.insertUser(user);
		if (result > 0) {
			return "registerSuccess";
		}
		return "registerError";
	}

}
